package View;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.KeyStroke;
import javax.swing.ListSelectionModel;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import org.pushingpixels.substance.api.SubstanceLookAndFeel;
import org.pushingpixels.substance.api.skin.GeminiSkin;

import bean.TestCase;

import Controller.MainController;

public class MainView {
	private MainController controller = null;
	private JFrame mainFrame = null;
	private JPanel infoPanel = null;
	private JPanel recPanel = null;
	private JMenuBar menuBar = null;
	private JMenuItem menuOpen = null;
	private JMenuItem menuClose = null;
	private JFileChooser fileChooser = null;
	private JTable infoTable = null;
	private JTable recTable = null;
//	private TableModel recTableModel = null;
	private JButton submitButton = null;

	private List<TestCase> testCaseList = null;

	public MainView(MainController controller) {
		this.controller = controller;
		/*JFrame.setDefaultLookAndFeelDecorated(true);
		JDialog.setDefaultLookAndFeelDecorated(true);
		java.awt.EventQueue.invokeLater(new Runnable() {
			public void run() {
				SubstanceLookAndFeel.setSkin(new GeminiSkin());
			}
		});*/
		init();
		addActionListener();
	}

	public void setController(MainController controller) {
		this.controller = controller;
	}

	private void init() {
		mainFrame = new JFrame();
		infoPanel = new JPanel(new BorderLayout());
		recPanel = new JPanel(new BorderLayout());
		fileChooser = new JFileChooser();
		menuBar = new JMenuBar();

		JMenu fileMenu = new JMenu("File");
		menuOpen = new JMenuItem("Open");
		menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_0,
				InputEvent.CTRL_MASK));

		menuClose = new JMenuItem("Close");
		menuClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,
				InputEvent.CTRL_MASK));
		fileMenu.add(menuOpen);
		fileMenu.addSeparator();
		fileMenu.add(menuClose);

		menuBar.add(fileMenu);

		infoTable = new JTable();
		infoTable.setRowSelectionAllowed(false);
		JScrollPane infoScroll = new JScrollPane(recTable);
		infoPanel.add(infoScroll, BorderLayout.CENTER);
		
		drawInfoTable();

		recTable = new JTable();
		recTable.setRowHeight(15);
		recTable.setRowSelectionAllowed(true);
		recTable.setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);

//		recTable.setDefaultRenderer(recTable.getColumnClass(0),
//				new TableCellRenderer() {
//					public Component getTableCellRendererComponent(
//							JTable table, Object value, boolean isSelected,
//							boolean hasFocus, int row, int column) {
//						String text = (value == null) ? "" : value.toString();
//						JLabel cell = new JLabel(text);
//						cell.setOpaque(true);
//
//						if (isSelected) {
//							Font font = cell.getFont();
//
//							cell.setFont(new Font(font.getFamily(), Font.BOLD,
//									font.getSize() + 3));
//						}
//
//						return cell;
//					}
//				});
		JScrollPane recScroll = new JScrollPane(recTable);
		recPanel.add(recScroll, BorderLayout.CENTER);
		
		submitButton = new JButton("Submit");
		recPanel.add(submitButton, BorderLayout.SOUTH);
		
		drawRecmendationTable();
		
		mainFrame.setJMenuBar(menuBar);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.getContentPane().add(recPanel, BorderLayout.EAST);
		mainFrame.getContentPane().add(infoPanel, BorderLayout.WEST);

		mainFrame.pack();
		Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
		int frameWidth = screenSize.width / 2;
		int frameHeight = screenSize.height / 2;
		mainFrame.setSize(frameWidth, frameHeight);
		int xLocation = screenSize.width / 2 - frameWidth / 2;
		int yLocation = screenSize.height / 2 - frameHeight / 2;
		mainFrame.setLocation(xLocation, yLocation);
		mainFrame.setResizable(false);
		mainFrame.setVisible(true);
		mainFrame.setTitle("Recommendation System");
	}

	private void addActionListener() {
		submitButton.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				int[] indexes = recTable.getSelectedColumns();
				List<Integer> sts = new ArrayList<Integer>();
				for (int i = 0; i < indexes.length; i++) {
					sts.add(indexes[i]);
				}
				controller.setResultsOfTestCases(sts);
			}
		});

		menuOpen.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				open();
			}
		});
		
		this.menuClose.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
	}

	private void open() {
		int option = fileChooser.showDialog(null, null);

		if (option == JFileChooser.APPROVE_OPTION) {
			File file = fileChooser.getSelectedFile();
			if (file.exists()) {
				controller.readData(file.getAbsolutePath());
			}
		}
	}
	
	private void drawInfoTable() {
	}

	private void drawRecmendationTable() {
		final int row;
		if (null == testCaseList || testCaseList.size() == 0) {
			row = 0;
		} else {
			row = testCaseList.size();
		}

		TableModel recTableModel = new AbstractTableModel() {
			private static final long serialVersionUID = 5679993447283210797L;

			@Override
			public int getColumnCount() {
				return 1;
			}

			@Override
			public int getRowCount() {
				return row;
			}

			@Override
			public Object getValueAt(int row, int column) {
				if (null != testCaseList && testCaseList.size() != 0) {
					TestCase tc = testCaseList.get(row);
					return tc.toString();
				} else {
					return null;
				}
			}

		};
		recTable.setModel(recTableModel);
		recTable.getTableHeader().getColumnModel().getColumn(0).setHeaderValue("Recommended TestCase");
	}

	private void repain() {
		
		
		
//		this.recPanel.removeAll();
//		this.infoPanel.removeAll();
		this.drawInfoTable();
		this.drawRecmendationTable();
	}
	
	public void refleshRecomendationTable(List<TestCase> testCaseList) {
		this.testCaseList = testCaseList;
		this.repain();
		mainFrame.validate();
	}
}
